<?php

session_start();
require( 'util.php' );

if (is_logged_as_monitor())
{
    validate_action_get( array( 'pid', 'action' ) );
    $pid = $_GET['pid'];
    
    require( 'db.php' );
    
    // 获取板块 ID
	$sql = "SELECT m_id FROM `page` WHERE p_id = $pid";	
	$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
	$row = mysql_fetch_array( $result ) or report_err_and_exit( '指定帖子不存在' );
    $mid = $row[0];
    
    switch ($_GET['action'])
    {
    case 'delete':
    
        delete_page( $pid );
        $action_desc = '成功删除了指定帖子';
        break;
        
    case 'top':
    
        set_page_on_top( $pid );
        $action_desc = '成功置顶了指定帖子';
        break;
    
    case 'notice':

        set_page_as_notice( $pid );
        $action_desc = '成功将帖子指定为公告';
        break;
    
    default:
    
        report_err_and_exit( '未知操作' );
        break;
    }

    // TODO: 页码
    redirect_and_exit( "list.php?mid=$mid", $action_desc );
}
else
{
    report_err_and_exit( '必须以版主的身份登录' );
}

function delete_page($pid)
{
    $sql = "DELETE FROM `comment` WHERE p_id = $pid";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );

    $sql = "DELETE FROM `page` WHERE p_id = $pid";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
}

function set_page_on_top($pid)
{
    modify_page_type( $pid, 'T' );
}

function set_page_as_notice($pid)
{
    modify_page_type( $pid, 'N' );
}

function modify_page_type($pid, $type)
{
    $sql = "SELECT type FROM page WHERE p_id = $pid";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
	$row = mysql_fetch_array( $result );
	
	$type = ($row['type'] == $type) ? 'O' : $type;

    $sql = "UPDATE page SET type = '$type' WHERE p_id = $pid";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
}

?>